//通过vue-router插件实现模板路由配置
import { createRouter, createWebHashHistory } from 'vue-router'
import { routes } from './routes';
import { ElMessage } from 'element-plus'
//创建路由器
const router = createRouter({
    //路由模式根据需求选择
    history: createWebHashHistory(),
    routes: routes,
})
//路由拦截
router.beforeEach((to, from, next) => {
    //判断是否需要登录
    if (to.meta.isAuth) {
        if (localStorage.getItem('token')) {
            next();
        } else {
            ElMessage({
                message: '请先登录',
                type: 'error',
            });
            setTimeout(() => {
                next('/login');
            }, 1000);
        }
    } else {
        next();
    }

})
export default router